/*
 * Copyright 2013-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.zimu.h.hexowriter.controller;

import com.zimu.h.hexowriter.config.HexoExecutor;
import com.zimu.h.hexowriter.config.HexoWriterConfig;
import com.zimu.h.hexowriter.entity.FileSaveRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author zimu
 */
@Controller
@CrossOrigin(origins = "*")
public class BasicController {
    @GetMapping("/")
    public String index() {
        return "index.html";
    }
    @GetMapping("/index")
    public String html() {
        return "index.html";
    }

    @PostMapping(value = "/save")
    @ResponseBody
    public String hello(@RequestBody FileSaveRequest fileSaveRequest) {
        File file = new File( HexoWriterConfig.HEXO_POST_DIR + "\\" + fileSaveRequest.getFilename());
        File parentFile = file.getParentFile();
        if ( !parentFile.exists() ){
            boolean mkdirs = parentFile.mkdirs();
            if (mkdirs){
                System.out.println("创建目录成功");
            }else {
                System.out.println("创建目录失败");
                return "失败";
            }
        }
        if (!file.exists()){
            try {
                boolean newFile = file.createNewFile();
                if (newFile){
                    System.out.println("创建文件成功");
                }else {
                    System.out.println("创建文件失败");
                    throw new RuntimeException("写入文件失败");
                }
            } catch (IOException e) {
                System.out.println("创建文件失败");
                e.printStackTrace();
                throw new RuntimeException("写入文件失败" + e.getMessage());
            }
        }
        try( FileOutputStream fileOutputStream = new FileOutputStream(file); ) {
            fileOutputStream.write(fileSaveRequest.getContent().getBytes());
            try {
                int exitCode = HexoExecutor.executeHexoCommand();
                if (exitCode == 0){
                    System.out.println("命令执行成功");
                    return "成功";
                }else {
                    System.out.println("命令执行失败，返回码: " + exitCode);
                    throw new RuntimeException("命令执行错误");
                }
            }catch (IOException | InterruptedException e){
                throw new RuntimeException("命令执行错误" + e.getMessage());
            }
        } catch (IOException e) {
            System.out.println("写入文件失败");
            e.printStackTrace();
            throw new RuntimeException("写入文件失败" + e.getMessage());
        }
    }



}
